import urllib.request
import urllib.parse
import os
import time
import random
import uuid
import re

# 目标PDF文件名
pdf_filename = '人工智能对就业市场的影响数据集.pdf'
# 保存路径
save_dir = r'd:\D'
pdf_save_path = os.path.join(save_dir, pdf_filename)

# 基础URL模板
base_url_template = "https://moor-smile.shec.edu.cn/rest/pdf/20250808/{uuid}/{filename}"

# 备用URL模板
alternative_url_templates = [
    "https://moor-smile.shec.edu.cn/api/pdf/20250808/{uuid}/{filename}",
    "https://moor-smile.shec.edu.cn/files/pdf/20250808/{uuid}/{filename}",
    "https://moor-smile.shec.edu.cn/pdf/20250808/{uuid}/{filename}"
]

# 用户提供的UUID
given_uuids = [
    "84519de1-8bb4-4bd1-b3fa-e610b20d4408",
    "50b9bf1c-287f-4a58-baa9-071f2db350a6",
    "5df60712-8a75-4581-814e-16a203dd27bb"
]

# 分析给定UUID的字符分布，创建概率矩阵
def analyze_uuid_patterns(uuids):
    # 移除连字符
    cleaned_uuids = [uuid.replace('-', '') for uuid in uuids]
    
    # 检查所有UUID长度是否一致
    if len(set(len(u) for u in cleaned_uuids)) != 1:
        print("警告: UUID长度不一致")
        return None
    
    uuid_length = len(cleaned_uuids[0])
    
    # 为每个位置创建字符频率统计
    position_stats = {}
    for i in range(uuid_length):
        position_stats[i] = {}
        
    # 统计每个位置上每个字符的出现次数
    for u in cleaned_uuids:
        for i, char in enumerate(u):
            if char in position_stats[i]:
                position_stats[i][char] += 1
            else:
                position_stats[i][char] = 1
    
    # 转换为概率
    for i in range(uuid_length):
        total = sum(position_stats[i].values())
        for char in position_stats[i]:
            position_stats[i][char] = position_stats[i][char] / total
    
    return position_stats

# 基于概率矩阵生成新的UUID
def generate_uuid_from_pattern(stats, uuid_count=100):
    if not stats:
        return []
    
    generated_uuids = []
    uuid_length = len(stats)
    
    for _ in range(uuid_count):
        new_uuid = ''
        for i in range(uuid_length):
            # 获取当前位置的字符和概率
            chars = list(stats[i].keys())
            probs = list(stats[i].values())
            
            # 根据概率选择字符
            # 由于只有3个样本，概率分布可能不够明显，所以这里使用均匀分布
            new_uuid += random.choice(chars)
        
        # 添加连字符，恢复标准UUID格式
        formatted_uuid = new_uuid[:8] + '-' + new_uuid[8:12] + '-' + new_uuid[12:16] + '-' + new_uuid[16:20] + '-' + new_uuid[20:]
        generated_uuids.append(formatted_uuid)
    
    return generated_uuids

# 生成各种类型的UUID
def generate_all_possible_uuids():
    all_uuids = []
    
    # 添加用户提供的UUID
    all_uuids.extend(given_uuids)
    
    # 分析用户提供的UUID模式并生成新的
    stats = analyze_uuid_patterns(given_uuids)
    if stats:
        pattern_uuids = generate_uuid_from_pattern(stats, 50)
        all_uuids.extend(pattern_uuids)
    
    # 生成基于日期的UUID变体
    date_based_uuids = [
        f"20250808-{random_string(4)}-{random_string(4)}-{random_string(4)}-{random_string(12)}",
        f"{random_string(8)}-2025-0808-{random_string(4)}-{random_string(12)}",
        f"{random_string(8)}-{random_string(4)}-2025-{random_string(4)}-{random_string(12)}",
        f"{random_string(8)}-{random_string(4)}-{random_string(4)}-2025-{random_string(12)}",
        f"{random_string(8)}-{random_string(4)}-{random_string(4)}-{random_string(4)}-20250808",
    ]
    all_uuids.extend(date_based_uuids)
    
    # 生成完全随机的UUID
    random_uuids = [str(uuid.uuid4()) for _ in range(100)]
    all_uuids.extend(random_uuids)
    
    # 生成连续数字UUID
    for i in range(1, 21):
        serial_uuid = f"00000000-0000-0000-0000-{str(i).zfill(12)}"
        all_uuids.append(serial_uuid)
    
    # 生成特殊前缀UUID
    prefixes = ['ai', 'job', 'employ', 'market', 'impact', 'data', 'set', '2025']
    for prefix in prefixes:
        for i in range(10):
            prefix_uuid = f"{prefix}{random_string(8-len(prefix))}-{random_string(4)}-{random_string(4)}-{random_string(4)}-{random_string(12)}"
            all_uuids.append(prefix_uuid)
    
    # 确保所有UUID都是唯一的
    all_uuids = list(set(all_uuids))
    
    return all_uuids

# 生成随机字符串
def random_string(length):
    chars = "0123456789abcdef"
    return ''.join(random.choice(chars) for _ in range(length))

# 不同的HTTP请求头
user_agents = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/89.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/91.0.864.59",
    "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1",
    "Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1"
]

def download_pdf(url, headers):
    """尝试下载PDF文件"""
    try:
        print(f"尝试URL: {url}")
        req = urllib.request.Request(url, headers=headers)
        
        # 设置超时时间为10秒
        with urllib.request.urlopen(req, timeout=10) as response:
            # 检查响应状态码
            if response.status == 200:
                # 检查Content-Type是否为PDF
                content_type = response.getheader('Content-Type')
                if content_type and 'pdf' in content_type.lower():
                    # 读取并写入文件
                    data = response.read()
                    with open(pdf_save_path, 'wb') as out_file:
                        out_file.write(data)
                    
                    # 验证文件大小
                    if os.path.exists(pdf_save_path):
                        file_size = os.path.getsize(pdf_save_path)
                        if file_size > 100:  # 确保文件不为空
                            return True, file_size
                        else:
                            print(f"警告: 下载的文件可能是空的 (大小: {file_size} 字节)")
                            os.remove(pdf_save_path)
                            return False, 0
                else:
                    print(f"警告: 响应内容不是PDF文件 (Content-Type: {content_type})")
                    return False, 0
            else:
                print(f"HTTP错误: {response.status}")
                return False, 0
    except Exception as e:
        print(f"请求错误: {str(e)}")
        return False, 0

def main():
    print(f"开始尝试下载'{pdf_filename}'")
    print(f"保存路径: {pdf_save_path}")
    
    # 生成所有可能的UUID组合
    uuid_combinations = generate_all_possible_uuids()
    print(f"生成的UUID数量: {len(uuid_combinations)}")
    print(f"尝试URL模板数量: {1 + len(alternative_url_templates)}")
    print(f"总共将尝试 {len(uuid_combinations) * (1 + len(alternative_url_templates))} 个URL组合")
    
    start_time = time.time()
    total_attempts = 0
    success = False
    
    # 尝试所有UUID组合和URL模板
    for uuid in uuid_combinations:
        if success:
            break
            
        # 随机选择用户代理
        headers = {'User-Agent': random.choice(user_agents)}
        
        # 尝试所有URL模板
        all_templates = [base_url_template] + alternative_url_templates
        for template in all_templates:
            if success:
                break
                
            # 编码文件名
            encoded_filename = urllib.parse.quote(pdf_filename)
            # 构建完整URL
            url = template.format(uuid=uuid, filename=encoded_filename)
            
            # 下载PDF
            total_attempts += 1
            success, file_size = download_pdf(url, headers)
            
            # 如果成功，退出循环
            if success:
                print(f"\nPDF文件下载成功！")
                print(f"保存路径: {pdf_save_path}")
                print(f"文件大小: {file_size / 1024:.2f} KB")
                print(f"使用的UUID: {uuid}")
                print(f"使用的URL模板: {template}")
                break
            
            # 随机延迟0.5-1.5秒，避免请求过于频繁
            time.sleep(random.uniform(0.5, 1.5))
    
    end_time = time.time()
    
    if not success:
        print(f"\n所有URL尝试都失败了。")
        print(f"总共尝试了 {total_attempts} 个URL组合")
        print(f"可能的原因:")
        print("1. 文件可能不存在或已被移动")
        print("2. 服务器可能限制了访问权限")
        print("3. 或者需要特定的请求头或认证信息")
        print("4. UUID格式可能不是我们猜测的样式")
        print("\n建议获取包含此PDF链接的HTML文件，或者确认文件是否存在于服务器上。")
    
    print(f"总耗时: {end_time - start_time:.2f} 秒")

if __name__ == "__main__":
    main()